package com.s3.khospitalds.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.s3.khospitalds.entity.Drugs;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
  * @Author: xw
  * @CreateTime: 2025/2/26
  * @Description: 药品信息表
  * @Version: 1.0
  */
@Mapper
@Repository
public interface DrugsMapper extends BaseMapper<Drugs> {
    @Select("SELECT * FROM drugs WHERE `type`='中药'")
    public List<Drugs> getDrugs();
    @Select("SELECT * FROM drugs WHERE `type`='西药'")
    public List<Drugs> getDrugsW();
    @Select("SELECT * FROM drugs WHERE `type`='中药' AND drugname LIKE concat('%', #{type} ,'%')")
    public List<Drugs> getDrugsByTypeLike(@Param("type") String type);
    @Select("SELECT * FROM drugs WHERE `type`='西药' AND drugname LIKE concat('%', #{type} ,'%')")
    public List<Drugs> getDrugsByTypeLikeW(@Param("type") String type);
    //补给中药
    @Insert("INSERT INTO drugs (drugname,OTC,form,specification,quantity,applicant,department,apply_time,expiry_date,vender,price)\n" +
            "VALUE(#{drugname},#{otc},#{form},#{specification},#{quantity},#{applicant},#{department},#{applyTime},DATE_ADD(NOW(),INTERVAL 3 MONTH),'白兰地有限公司',#{price})")
    public int insertDrugs(Drugs drugs);
    //查询数量小于10的中药
    @Select("SELECT * FROM drugs WHERE quantity<10 AND quantity>=0 AND `type`='中药';")
    public List<Drugs> getDrugsByQuantity();

    //补给西药
    @Insert("INSERT INTO drugs (`type`,drugname,OTC,form,specification,quantity,applicant,department,apply_time,expiry_date,vender,price)\n" +
            "VALUE('西药',#{drugname},#{otc},#{form},#{specification},#{quantity},#{applicant},#{department},#{applyTime},DATE_ADD(NOW(),INTERVAL 3 MONTH),'白兰地有限公司',#{price})")
    public int insertDrugsW(Drugs drugs);
    //查询数量小于10的西药
    @Select("SELECT * FROM drugs WHERE quantity<10 AND quantity>=0 AND `type`='西药';")
    public List<Drugs> getDrugsByQuantityW();

    @Select("SELECT * FROM drugs WHERE drugname LIKE concat('%', #{drugname} ,'%')")
    public List<Drugs> getDrugsByDrugNameLike(@Param("drugname") String drugname);
    @Update("UPDATE drugs SET quantity=quantity-#{quantity} WHERE id=#{id} AND quantity>0;")
    int updateDrugsQuantity(Drugs drugs);

    //修改中药物
    @Update("UPDATE drugs SET quantity= quantity + #{quantity} WHERE drugname = #{drugname}")
    public int updateDrugs(@Param("drugname") String drugname,@Param("quantity") int quantity);

    //修改西药物
    @Update("UPDATE drugs SET quantity= quantity + #{quantity} WHERE drugname = #{drugname} and type = '西药'")
    public int updateDrugsW(@Param("drugname") String drugname,@Param("quantity") int quantity);
}
